package com.sakura.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakura.base.common.model.PageParams;
import com.sakura.base.common.model.PageResult;
import com.sakura.content.mapper.ElderlyMapper;
import com.sakura.content.service.ElderlyService;
import com.sakura.elderly.dto.ElderlyQueryParamDto;
import com.sakura.model.entity.Elderly;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sakura
 */
@Slf4j
@Service
public class ElderlyServiceImpl extends ServiceImpl<ElderlyMapper, Elderly> implements ElderlyService {

    @Autowired
    private ElderlyMapper elderlyMapper;

    @Override
    public PageResult<Elderly> queryElderlyBaseList(PageParams pageParams, ElderlyQueryParamDto queryParamDto) {
        LambdaQueryWrapper<Elderly> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(queryParamDto.getName()), Elderly::getName, queryParamDto.getName());
        // 分页对象
        Page<Elderly> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<Elderly> elderlyPage = elderlyMapper.selectPage(page, queryWrapper);

        List<Elderly> records = elderlyPage.getRecords();

        long total = elderlyPage.getTotal();

        return new PageResult<>(records, total, pageParams.getPageNo(), pageParams.getPageSize());
    }
}
